package edu.vt.ece_3574.amber_alert.database;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;

import android.os.AsyncTask;
import android.util.Log;
import edu.vt.ece_3574.amber_alert.accounts.Account;

public class AddAccountToServerHandler extends AsyncTask<Void, Void, String> {
	private Account account;
	public static final String successfullyAddedString = "Success";
	public static final String failureString = "Failure";
	public static final String userExistedString = "DuplicateUser";

	public AddAccountToServerHandler(Account account) {
		this.account = account;
	}

	@Override
	protected String doInBackground(Void... params) {
		
		InputStream is = null;
		String result = "";
		
		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("serialNum", account.getSerialNumber()));
		nameValuePairs.add(new BasicNameValuePair("name", account.getName()));
		nameValuePairs.add(new BasicNameValuePair("password", account.getPassword()));
		nameValuePairs.add(new BasicNameValuePair("phoneNumber", account.getNumber()));
		nameValuePairs.add(new BasicNameValuePair("type", account.getType()));

		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(DatabaseHandler.databaseURL + "addAccount.php");
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost); 
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
		}
		catch(Exception e) {
			Log.e("log_tag", "Error in http connection " + e.toString());
		}
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			result=sb.toString();
		}
		catch (Exception e) {
			Log.e("log_tag", "Error converting result "+e.toString());
		}
		//parse json data
		try{
//			JSONArray jArray = new JSONArray(result);
			new JSONArray(result);
//			for(int i=0;i<jArray.length();i++) {
//				JSONObject json_data = jArray.getJSONObject(i);
//				Log.i("log_tag","id: "+json_data.getString("SerialNumber") + ", name: "+json_data.getString("Name") + ", type: "+json_data.getString("Type"));
//			}
		}
		catch (JSONException e) {
			if (e.toString().contains("User added to database")) {
				return successfullyAddedString;
			}
			else if (e.toString().contains("User already existed in database")) {
				return userExistedString;
			}
			else {
				return failureString;
			}
		}
		return successfullyAddedString;
	}
}
